﻿@model thailandProject.Models.USER

@{
    ViewBag.Title = "Edit User";
}

<div id="Wrapper2">
    <div id="content_header">
        <div id="headerimage">
        </div>
        <div id="theheader">@ViewBag.Header</div>
    </div>
    <!-- Left Column begins here -->
    <aside id="sidebar-wrapper">
        <nav class="sidebar">
            <!-- CONTENT GOES HERE -->
            @using(Html.BeginForm("EditUser", "Admin", FormMethod.Post, new { id = "addForm" }))
            {
                int sessionUserID = ((thailandProject.Models.USER)Session["user"]).userID;
                <div id="searchcontainer">
                <div class="display-label">Username</div>
                <div class="display-field">
                    <input type="hidden" name="userID" value="@Model.userID" />
                    <input type="text" id="username" name="username" maxlength="50" value="@Model.username" />
                    <div id="usernameError" style="color:red; visibility:hidden">Username must not be empty</div>
                </div>
                <div class="display-label">First Name</div>
                <div class="display-field">
                    <input type="text" id="userFirstName" name="userFirstName" maxlength="50" value="@Model.userFirstName" />
                    <div id="userFirstNameError" style="color:red; visibility:hidden">First name must not be empty</div>
                </div>
                <div class="display-label">Last Name</div>
                <div class="display-field">
                    <input type="text" id="userLastName" name="userLastName" maxlength="50" value="@Model.userLastName" />
                    <div id="userLastNameError" style="color:red; visibility:hidden">Last name must not be empty</div>
                </div>
                <div class="display-label">Email</div>
                <div class="display-field">
                    <input type="text" id="userEmail" name="userEmail" maxlength="50" value="@Model.userEmail" />
                    <div id="userEmailError" style="color:red; visibility:hidden">Email must not be empty</div>
                </div>
                <div class="display-label">Password</div>
                <div class="display-field">
                    <input type="password" id="userPassword" name="userPassword" maxlength="2048" value="@Model.userPassword" /><!-- Long password, appearently -->
                    <div id="userPasswordError" style="color:red; visibility:hidden">Password must not be empty</div>
                </div>
                <div class="display-label">Is Active</div>
                <div class="display-field">
                    <input type="checkbox" id="isActive" name="isActive" @if(Model.isActive.HasValue && Model.isActive.Value) { @:checked="checked"
                    }@if(sessionUserID == Model.userID) { @:disabled="disabled"
                    }/>
                </div>
                <br />
                <div class="display-label">Permission Level</div>
                <div class="display-field">
                    <select name="permID" @if(sessionUserID == Model.userID) { @:disabled="disabled"
                        }>
                        <option value="3" @if(Model.permID == 3) { @:selected="selected"
                        }>Viewer</option>
                        <option value="2" @if(Model.permID == 2) { @:selected="selected"
                        }>Staff</option>
                        <option value="1" @if(Model.permID == 1) { @:selected="selected"
                        }>Admin</option>
                    </select>
                </div>
                

                <div class="formButtons">
                    <input type="image" src="../../images/save_button.png" id="addTaskTypeButton" value="Save User" />
                    <div id="formError" style="color:red; visibility:hidden">Please fix the above errors to continue</div>
                </div>
                @Html.ActionLink("Back to List", "ManageUsers")
            </div>
            }
        </nav>
    </aside>
    <!-- Right column begins here -->
    <article id="contents">
        <div>
            <fieldset>
                <legend>PERMISSION INFO</legend>
                Viewer &mdash; Can only view information. Cannot edit or add.<br />
                Staff &mdash; Capable of everything except Admin features<br />
                Admin &mdash; Able to access every feature of the system
                </fieldset>
        </div>
    </article>
    <!-- Right column ends here -->
</div>
<!-- Div wrapper2 ends here -->

<script type = "text/javascript">
    $("#addForm").submit(function () {

        var isValid = true;

        if ($("#username").val() == "") {
            $("#usernameError").attr('style', 'color:red; visibility:visible;');
            isValid = false;

            $("#username").focus();
        }
        else
            $("#usernameError").attr('style', 'color:red; visibility:hidden;');

        if ($("#userFirstName").val() == "") {
            $("#userFirstNameError").attr('style', 'color:red; visibility:visible;');
            if (isValid)
                $("#userFirstName").focus();

            isValid = false;
        }
        else
            $("#userFirstNameError").attr('style', 'color:red; visibility:hidden;');

        if ($("#userLastName").val() == "") {
            $("#userLastNameError").attr('style', 'color:red; visibility:visible;');
            if (isValid)
                $("#userLastName").focus();

            isValid = false;
        }
        else
            $("#userLastNameError").attr('style', 'color:red; visibility:hidden;');

        if ($("#userEmail").val() == "") {
            $("#userEmailError").attr('style', 'color:red; visibility:visible;');
            if (isValid)
                $("#userEmail").focus();

            isValid = false;
        }
        else
            $("#userEmailError").attr('style', 'color:red; visibility:hidden;');

        if ($("#userPassword").val() == "") {
            $("#userPasswordError").attr('style', 'color:red; visibility:visible;');
            if (isValid)
                $("#userPassword").focus();

            isValid = false;
        }
        else
            $("#userPasswordError").attr('style', 'color:red; visibility:hidden;');

        if (!isValid) {
            $("#formError").attr('style', 'color:red; visibility:visible;');
            return false;
        }
        else {
            $("#formError").attr('style', 'color:red; visibility:hidden;');
            return true;
        }
    })
</script>
